// pages/my/my.js
var app = getApp()
Page({
  data: {
    agentId:'',
    accountId:'',
    putInCart:false,
    goodsAttr:[],
    currentItem:'',
    currentIndex:'',
    currentBox:[],
    isStock:false,
    enterCart:true,
    //商品信息
    id:'',name:'',imgUrl:'',price:'',sales:'',stock:'',productId:'',goodsType:'',thumbUrl:'',product_type:'',applyTo:'',versionValues:'',versionNameId:'',
    //厂家信息
    brand_url:'',brand_desc:'',brand_name:'',brand:true,
    //配送信息
    logistics:'',
    //商品详情
    details:[],
    num:1,
    actPrice:'',
    totalPrice:'',
    versionValueIds:'',
    ver_value_ids:'',
    typeDetailList:'',
    product_code:'',
    color:'',
    companyName:'',
    searchType:'',
    productType:'',
    shareInfo:''
  },
  onLoad: function (options) {
    let shareInfo = options
    let mark = options.mark
    let goodsType = options.goodsType, //一级分类
    productType = options.product_type,//二级分类
    goodsId = options.id,
    mtId = options.mtId,
    applyTo = options.applyTo,
    goodsPrice = options.price=="null"?0:options.price
    this.setData({
      price:goodsPrice,
      totalPrice:goodsPrice,
      productId:goodsId,
      goodsType,
      productType,
      applyTo,
      mtId,
      shareInfo,
      color:options.color,
      companyName:options.companyName
    })

    app.getMemInfo().then(res=>{
      let agentId = res.data.agentId
      let accountId = res.data.id
      if (mark==1) {
        this.getGoodsDetail2(agentId,goodsType,goodsId)
      }else{
        this.getGoodsDetail(agentId,goodsType,goodsId)
      }
      this.setData({
        agentId,
        accountId
      })
    })
    if (options.type==1) {
      this.setData({
        isStock:true
      })
    }

  },
  onShareAppMessage: function (res) {
    return {
      title: this.data.name,
      path: '/pages/stock/detailShare?type=1&goodsType='+this.data.shareInfo.goodsType+
          '&id='+this.data.shareInfo.id+
          '&price='+this.data.shareInfo.price+
          '&companyName='+this.data.shareInfo.companyName+
          '&color='+this.data.shareInfo.color+
          '&product_type='+this.data.shareInfo.productType+
          '&applyTo='+this.data.shareInfo.applyTo+
          '&mtId='+this.data.shareInfo.mtId,
      imageUrl: this.data.imgUrl,
      success: function (res) {
	        wx.showToast({
	          title: "分享成功",
	          icon: 'success',
	          duration: 2000
	        })
       },
      fail: function (res) {

      },
    }
  },

  bindBuy(){
    this.setData({
      // putInCart:true,
      enterCart:false
    })
  },
  //加入购物车
  bindEnterCart(){
    this.setData({
      // putInCart:true,
      enterCart:true
    })
  },
  bindClose(){
    this.setData({
      putInCart:false
    })
  },
  bindGoToCart(){
    wx.navigateTo({
      url:'settlement'
    })
  },
  bindChoseAttr(e){
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    let ids = e.currentTarget.dataset.ids
    let currentBox = this.data.currentBox
    const that = this
    currentBox.forEach((item,i)=>{
      if (item.id==id) {
        item.current=true
      }else{
        item.current=false
      }
    })
    console.log(ids)
    this.setData({
      currentBox,
      versionValueIds:id,
      ver_value_ids:ids,
      currentItem:name
    })
    this.getPrice(this.data.versionValueIds)
  },
  //根据商品规格获取库存信息，价格，库存量
  getPrice(){
    // this.getVersions()
    var that = this
    let data = {
      skuId:this.data.id,
      agentId:this.data.agentId,
      type:this.data.goodsType
    }
    console.log(data)
    app.request('wx_app/mall/product/detail',data,'GET').then(res=>{
      console.log(res)
      that.setData({
        stock:res.data.stock,
        actPrice:res.data.price,
        price:res.data.price,
        thumbUrl:res.data.thumbUrl,
        totalPrice:res.data.price*that.data.num
      })
    })

  },
  getVersions(){
    let versions = []
    let verSort = []
    let versionValueIds =''
    this.data.currentBox.forEach(item=>{
      this.setData({
        versionValueIds:item.id
      })
    })
    // versions.forEach(item=>{
    //   if (item!='') {
    //     verSort.push(parseInt(item))
    //   }
    // })
    // verSort.sort().forEach(item=>{
    //   versionValueIds+=item+','
    // })
    // this.setData({
    //   versionValueIds:versionValueIds.substr(0,versionValueIds.length-1)
    // })
  },
  //初始化商品规格
  creatCurrentBox(agentId,type,productId){
    var that = this
    var data={
      agentId,
      type,
      productId
    }
    console.log(data)
    app.request('wx_app/mall/product/version/detail',data,'GET').then(res=>{
      console.log(res)
      let goodsAttr = []
      res.data.versions.forEach((item,i)=>{
        goodsAttr.push({
          name:item.versionValue,
          id:item.sku_product_id,
          ids:item.versionValueIds
        })
      })
      that.setData({
        goodsAttr
      })
      //获取默认属性放入currentBox
      let currentBox = []
      that.data.goodsAttr.forEach((item,i)=>{
        currentBox.push({
          name:item.name,
          id:item.id,
          current:false
        })
      })
      currentBox[0].current=true
      that.setData({
        currentBox,
        ver_value_ids:res.data.versions[0].versionValueIds,
        versionValueIds:res.data.versions[0].sku_product_id
      })
      // let typeList=''
      // that.data.currentBox.forEach(item=>{
      //   typeList+=item.item.id+','
      // })
      // that.setData({
      //   versionValueIds:typeList.substr(0, typeList.length - 1)
      // })
      this.getPrice(res.data.versions[0].sku_product_id)
    })
  },
  //将物品加入购物车
  bindPutInCart(){
    this.setData({
      putInCart:false
    })
    let data = {
      productType:this.data.productType,
      accountId:this.data.accountId,
      skuId:this.data.id,
      productName:this.data.name,
      price:this.data.price,
      count:1,
      versionValues:this.data.versionValues,
      versionNameId:this.data.versionNameId,
      isOldForNew:0,
      applyTo:this.data.applyTo,
      mtId:this.data.mtId
    }
    console.log(data)
    app.request('wx_app/cart/product/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.showToast({
          title:'加入购物单成功！'
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //结算
  bindToCart(){
    let data = {
      productType:this.data.productType,
      accountId:this.data.accountId,
      skuId:this.data.id,
      productName:this.data.name,
      price:this.data.price,
      count:1,
      versionValues:this.data.versionValues,
      versionNameId:this.data.versionNameId,
      isOldForNew:0,
      applyTo:this.data.applyTo,
      mtId:this.data.mtId
    }
    console.log(data)
    app.request('wx_app/cart/product/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.navigateTo({
          url:'settlement'
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //获得商品详情
  getGoodsDetail(agentId,type,skuId){
    // wx.showLoading({
    //   title:'正在加载'
    // })
    var data = {
      agentId,
      type,
      skuId
    }
    console.log(data)
    var that = this
    app.request('wx_app/mall/product/detail',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let details = []
        details.push(
          res.data.sku.imgUrl2,
          res.data.sku.imgUrl3,
          res.data.sku.imgUrl4,
          res.data.sku.imgUrl5,)
        that.setData({
          id:res.data.sku.id,
          name:res.data.sku.productName,
          imgUrl:res.data.sku.mainImgUrl,
          sales:res.data.sku.sales,
          stock:res.data.sku.stock,
          brand_url:res.data.brand_url?res.data.brand_url:'',
          brand_desc:res.data.brand_desc?res.data.brand_desc:'',
          brand_name:res.data.brand_name?res.data.brand_name:'',
          applyTo:res.data.sku.applyTo,
          versionValues:res.data.sku.versionValues,
          versionNameId:res.data.sku.versionNameId,
          details,
          product_type:res.data.sku.versionValues+" "+res.data.sku.colorName,
          searchType:res.data.sku.productType
      })
        if (!res.data.brand_name) {
          that.setData({
            brand:false
          })
        }else{
          that.setData({
            brand:true
          })
        }
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  getGoodsDetail2(agentId,type,productId){
    wx.showLoading({
      title:'正在加载'
    })
    var data = {
      agentId,
      type,
      productId
    }
    console.log(data)
    var that = this
    app.request('wx_app/mall/product/order_detail',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let details = []
        res.data.details.forEach(item=>{
          details.push(item.url)
        })
        that.setData({
          name:res.data.product_name,
          imgUrl:res.data.url,
          sales:res.data.sales,
          stock:res.data.stock,
          brand_url:res.data.brand_url?res.data.brand_url:'',
          brand_desc:res.data.brand_desc?res.data.brand_desc:'',
          brand_name:res.data.brand_name?res.data.brand_name:'',
          typeDetailList:res.data.apply_to==null?false:res.data.apply_to,
        // logistics:res.data.logistics,
          product_code:res.data.product_code?res.data.product_code:'暂无',
          details
      })
        if (!res.data.brand_name) {
          that.setData({
            brand:false
          })
        }else{
          that.setData({
            brand:true
          })
        }
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  add(e){
    let num = e.currentTarget.dataset.num
    num++
    let price = this.data.actPrice
    this.setData({
      num,
      totalPrice:this.data.actPrice?this.data.actPrice*num:this.data.price*num
    })
  },
  sub(e){
    let num = e.currentTarget.dataset.num
    num--
    let price = this.data.actPrice

    if (num>0) {
      this.setData({
        num,
        totalPrice:price*num
      })
    }
  },
})
